from flask import Blueprint
from flask_restful import Resource, Api,reqparse
from flask import request, json, jsonify
from common.utils.jwt_token import create_token
from second_flask.methods import db1

shop_bp = Blueprint('shop_bp', __name__)

api = Api(shop_bp)


class Login(Resource):
    def post(self):
        data = request.data
        if data:
            data = json.loads(data)
        else:
            data = request.form
        sql = "select * from gusers where username='%s' and password='%s'" % (data['username'], data['password'])
        user = db1.find(sql)
        print('1111111', user)
        user_id = user['id']
        print('>>>>>', user_id)
        resql = "select re.url,re.name from roles_resource as rr inner join resource as re on rr.resourceid=re.id where rr.roleid='%d'" % (
            user['role_id'])
        resource_list = db1.find_all(resql)
        token, refresh_token = create_token(user_id)
        username = user['username']
        user_id = user['id']
        print('444444', user_id)
        if user:
            return jsonify({'code': 200, 'message': '登录成功', 'token': token, 'refresh_token': refresh_token,
                            'resource_list': resource_list, 'username': username, 'user_id': user_id})
        else:
            return jsonify({'code': 200, 'message': '没有该用户'})


class GetGoodslist(Resource):
    def get(self):
        sql = "select * from ugoods"
        goodslist = db1.find_all(sql)
        return jsonify({'code': 200, 'goodslist': goodslist})


class AddOrder(Resource):
    def post(self):
        data = request.data
        if data:
            data = json.loads(data)
        else:
            data = request.form
        sql = "insert into uorders(goods_name,tprice,goods_count,user_id) values('%s','%s','%d','%s')" % (
            data['goods_name'], data['tprice'], data['goods_count'], data['id'])
        db1.update(sql)
        return jsonify({'code': 200})


class GetuserOrder(Resource):
    def get(self,id):
        # data = request.data
        # if data:
        #     data = json.loads(data)
        # else:
        #     data = request.form
        # data = request.args.get('id')
        # parse = reqparse.RequestParser()
        # parse.add_argument('id')
        # args = parse.parse_args()
        # id = args.get('id')
        # print('data',args)
        # print('55555',data['id'])
        # sql = "select * from uorders where user_id=%d" % (int(data['id']))
        sql = "select * from uorders where user_id=%d" % (int(id))
        orders = db1.find_all(sql)
        return {'code': 200, 'order_list': orders}


class GetAllOrders(Resource):
    def get(self):
        sql = "select * from uorders"
        data = db1.find_all(sql)
        return jsonify({'code': 200, 'data': data})

class PutOrder(Resource):
    def put(self):
        data = request.data
        if data:
            data = json.loads(data)
        else:
            data = request.form
        sql = "update uorders set status=2 where id='%d'"%(data['id'])
        db1.update(sql)
        return jsonify({'code':200})


api.add_resource(Login, '/user/login')
api.add_resource(GetGoodslist, '/user/getgoodslist')
api.add_resource(AddOrder, '/user/addorder')
api.add_resource(GetuserOrder, '/user/getuserorder/<int:id>')
api.add_resource(GetAllOrders, '/user/getallorders')
api.add_resource(PutOrder, '/user/putorder')
